require('./connect.js')
const mongoose = require('mongoose')

// 创建集合
const schema = new mongoose.Schema({
  question: String,
  answer: String,
  analyse: String,
  source: String,
})
const Question = mongoose.model('Question', schema)

// 插入方式一
async function insert() {
  try {
    const course = new Question({
      question: '你的第一个编程语言是什么？',
      answer: 'Java',
      analyse: '在北京的时候',
      source: '学习Android的时候',
    })
    const result = await course.save()
    console.log(result, '保存成功')
  } catch (error) {
    console.log(error, '保存失败')
  }
}

// 插入方式二
async function insert2() {
  try {
    const result = await Question.create({
      question: '你的第一个编程科目是',
      answer: 'Android',
      analyse: '使用的是java语言',
      source: '学习Android的时候',
    })
    console.log(result, '保存成功')
  } catch (error) {
    console.log(error, '保存失败')
  }
}

// insert()
// insert2()

const userSchema = new mongoose.Schema({
  age: Number,
  email: String,
  hobbies: [String],
  name: String,
  password: String,
})
const User = mongoose.model('User', userSchema)

function insertUser(obj) {
  new User(obj)
    .save()
    .then((result) => console.log('插入成功', result))
    .catch((e) => console.log('插入失败', e))
}

/* for (let i = 0; i < 2; i++) {
  insertUser({
    age: 29,
    email: '270628297@qq.com',
    hobbies: ['象棋', '乒乓球'],
    name: '乐哥哥2',
    password: '888888'
  })
} */
